<?php

declare(strict_types=1);

namespace Berlin\Spider;

use Berlin\Spider\Result;
use Verdient\http\Request;
use Symfony\Component\DomCrawler\Crawler;

/**
 * 吉客印
 * @author Verdient.
 */
class Giikin
{
    /**
     * 获取商品
     * @param string $url URL地址
     * @return Result
     * @author Verdient。
     */
    public function fetchProduct($url): Result
    {
        $request = new Request();
        $request->setUrl($url);
        $request->setMethod('GET');
        $request->setHeaders([
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'
        ]);
        $response = $request->send();
        $content = $response->getRawContent();
        $encoding = $response->getHeaders()['content-encoding'] ?? null;
        if ($encoding === 'gzip') {
            $content = gzdecode($content);
        }

        //DOM操作
        $crawler = new Crawler($content);
        $images = $crawler->filter('.base-data p img')->each(function (Crawler $img, $i) {
            return trim($img->attr('src'));
        });
        $video = $crawler->filter('.base-data p video')->each(function (Crawler $img, $i) {
            return trim($img->attr('src'));
        });
        $banner = $crawler->filter('.swiper-slide img')->each(function (Crawler $img, $i) {
            return trim($img->attr('src'));
        });
        $promotionText = @$crawler->filter('.promotion-text span')->text('无');
        // var_dump($video);die;

        //js info
        preg_match('/ sinfo = \{.*?\};/', $content, $js_info);
        $js_info = str_replace(" sinfo = ", "", $js_info[0]);
        $js_info = json_decode(substr($js_info, 0, strlen($js_info) - 1), true);

        //币种
        preg_match('/curOpts.iniId = "\d+"/', $content, $currencyId);
        $currencyId = explode(' = "', $currencyId[0]);
        $currencyId = substr(end($currencyId), 0, strlen(end($currencyId)) - 1);
        $curData = '[{"id":"14","offset":"0","name":"SAR","symbol":"ريال","pos":0},{"id":"1","offset":"0","name":"AED","symbol":"درهم","pos":0},{"id":"25","offset":"0","name":"KWD","symbol":"KWD","pos":1}]';
        $curList = json_decode($curData, true);
        foreach ($curList as $v) {
            if ($v['id'] == $currencyId) {
                $currency = $v['name'];
                break;
            }
        }

        //规格
        $variants = [];
        foreach ($js_info['skus'] as $k => $skuInfo) {
            $variants[$k]['colour'] = $skuInfo['option'][0]['name'] ?? '';
            $variants[$k]['size'] = $skuInfo['option'][1]['name'] ?? '';
            $variants[$k]['image'] = 'https://pic.compgoo.com/' . $skuInfo['option'][0]['image'];
        }
        // var_dump($promotionText);die;

        return new Result([
            'title' => $js_info['title'],
            'name' => $js_info['name'],
            'promotionText' => $promotionText,
            'price' => $js_info['finalPrice'],
            'descriptionImage' => $images,
            'banner' => $banner,
            'videos' => $video,
            'currency' => $currency,
            'variants' => $variants,
        ]);
    }
}
